﻿
using ZFY.Blog.Api.IRepository;
using ZFY.Blog.Api.IServices;
using ZFY.Blog.Api.Model.Dto.ArticleTypeDto;
using ZFY.Blog.Api.Model.Model;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZFY.Core;
using ZFY.AspNetCore;

namespace ZFY.Blog.Api.Services
{
    public class ArticleTypeServices: ZFY.DDD.Application.ApplicationService, IArticleTypeServices
    {
        private readonly IArticleTypeRepository _articleTypeRepository;
        private readonly IMapper _mapper;

        public ArticleTypeServices(IArticleTypeRepository articleTypeRepository,IMapper mapper)
        {
            _articleTypeRepository = articleTypeRepository;
            _mapper = mapper;
        }

        public async Task<GetArticleTypeOutput> AddArticleTypeAsync(CreateArticleTypeInput input)
        {
            //input.Meun.Value.ValidMenuEnum();
            var articleType = _mapper.Map<ArticleType>(input);
            await ValidateArticleTypeNmae(articleType);
            await _articleTypeRepository.AddAsync(articleType);
            await _articleTypeRepository.SaveChangesAsync();
            return _mapper.Map<GetArticleTypeOutput>(articleType);
        }

        public async Task DeleteArticleTypeAsync(int value)
        {
            if ((await _articleTypeRepository.GetByIdIncludeNavAsync(value)) is ArticleType articleType)
            {
                if (articleType.Articles.Count>0)
                {
                    throw new ZFYException("存在属于该类型的文章，不能删除！");
                }
                if (!string.IsNullOrEmpty(articleType.Image))
                {
                    File.Delete(StaticInfo.WebRootPath + articleType.Image);
                }
                await _articleTypeRepository.DeleteAsync(articleType);
            }
        }
        public async Task<List<ArticleType>> GetArticleTypeListAsync(GetArticleTypeInput input)
        {
            IQueryable<ArticleType> query = await _articleTypeRepository.GetQueryableAsync();
            if (!string.IsNullOrWhiteSpace(input.Name))
            {
                query = query.Where(x => x.Name.Contains(input.Name));
            }
            if (input.Id.HasValue)
            {
                query = query.Where(x => x.Id.Equals(input.Id));
            }
            return await query.Include(x=>x.Articles).AsNoTracking().ToListAsync();
        }

        public async Task UpdateArticleTypeAsync(UpdateArticleTypeInput input)
        {
            if ((await _articleTypeRepository.FindAsync(input.Id.Value)) is ArticleType type)
            {
                _mapper.Map(input, type);
                await ValidateArticleTypeNmae(type);
                if (input.ImageFile != null)
                {
                    ReuseMethod.ImageFormatCheck(input.ImageFile.FileName);
                    var buffer = await ReuseMethod.GetFileBufferAsync(input.ImageFile);
                    File.WriteAllBytes(type.Image, buffer);
                }
            }
        }
        private async Task ValidateArticleTypeNmae(ArticleType articleType)
        {
            if (await _articleTypeRepository.AnyAsync(x => x.Name.Equals(articleType.Name)&&!x.Id.Equals(articleType.Id)))
            {
                throw new ZFYException("类型名称重复!");
            }
        }
    }
}
